﻿using Core.DataAccess.Model;
using Core.Framework;
using Dapper;
using Local.Interface;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Local.Implement
{
    public class CloudFunction : ICloudFunction
    {
        private IDBFactory iDBFactory;

        public CloudFunction(IDBFactory iDBFactory)
        {
            this.iDBFactory = iDBFactory;
        }

        public MethodResult<int> Add(Action<KeyValuePair<string, string>> action, Function function)
        {

            var result = new MethodResult<int>();

            using (var conn = this.iDBFactory.Create(DatabaseType.Function))
            {
                try
                {
                    // 检查是否已经存在1
                    var func = conn.QueryFirstOrDefault<Function>("select id from [Function] where moduleName = @moduleName", new { function.moduleName });

                    if (func?.id > 0)
                    {
                        result.Discription = "已存在当前 moduleName";
                        return result;
                    }

                    //int row = conn.Execute("INSERT INTO [Function]([moduleName],[modulePath],[scriptContent]) VALUES(@moduleName,@modulePath,@scriptContent)",new {
                    //    function.moduleName,
                    //    function.modulePath,
                    //    function.scriptContent
                    //});
                    var key = conn.Insert(function);

                    if (key > 0)
                    {
                        result.IsFinished = true;
                        result.Data = (int)key;

                        action.Invoke(new KeyValuePair<string, string>(function.modulePath, function.scriptContent));

                        //this.iProducter
                        //    .PublishAsync(
                        //    "scriptbudiler",
                        //    new KeyValuePair<string, string>(function.modulePath, function.scriptContent));
                    }
                    else
                    {
                        result.Discription = "系统异常.";
                    }
                }
                catch (Exception ex)
                {
                    result.Discription = ex.Message;
                }
            }

            return result;
        }

        public MethodResult<Pagination> GetFunctionList(Pagination pagination)
        {
            var result = new MethodResult<Pagination>();

            using (var db = this.iDBFactory.Create(DatabaseType.Function))
            {
                try
                {
                    var list = db.Query<Function>($"select moduleName,isUseState from [Function]");
                    pagination.records = list.Count();
                    pagination.Data = list;
                    result.IsFinished = true;
                    result.Data = pagination;
                }
                catch (Exception ex)
                {
                    result.Discription = ex.Message;
                }
            }

            return result;
        }



    }
}
